package edu.uta.tsrh.db;

import edu.uta.tsrh.db.commandpattern.*;
import edu.uta.tsrh.model.*;
import java.sql.Date;
import java.util.ArrayList;

public class MySQLDBServices
        implements DBServices {

    @Override
    public ArrayList<SearchResult> getSearchFileList(String fileID, String patientFirstName, String patientLastName,
            Date patientDateOfBirth, String fileStatus) {
        GetSearchFileList getSearchFiles = new GetSearchFileList(fileID, patientFirstName, patientLastName,
                patientDateOfBirth, fileStatus);
        ArrayList<SearchResult> searchResults = getSearchFiles.execute();
        return searchResults;
    }

    @Override
    public FileDetail getFileDetails(String fileID) {
        GetFileDetails getDetails = new GetFileDetails(fileID);
        
        FileDetail fileDetail = getDetails.execute();
        return fileDetail;
    }

    @Override
    public int insertTransaction(Transaction transaction) {
        InsertTransaction insert = new InsertTransaction(transaction);
        int result = Integer.parseInt((String) insert.execute());
        return result;
    }

    @Override
    public int updateFileLocation(String location, String fileState, String fileID) {
        UpdateFileLocation update = new UpdateFileLocation(location, fileState, fileID);
        int result = Integer.parseInt((String) update.execute());
        return result;
    }

    @Override
    public ArrayList<Transaction> getFileHistory(String fileID, String startTime, String endTime) {
        GetFileHistory history = new GetFileHistory(fileID, startTime, endTime);
        ArrayList<Transaction> transactions = history.execute();
        return transactions;
    }

    @Override
    public int insertPendingDate(PendingDate pendingDate) {
        InsertPendingDate insert = new InsertPendingDate(pendingDate);
        int result = Integer.parseInt((String) insert.execute());
        return result;
    }

    @Override
    public int updatePendingDate(PendingDate pendingDate) {
        UpdatePendingDate update = new UpdatePendingDate(pendingDate);
        int result = Integer.parseInt((String) update.execute());
        return result;
    }

    @Override
    public int deletePendingDate(int pendingDateID) {
        DeletePendingDate delete = new DeletePendingDate(pendingDateID);
        int result = Integer.parseInt((String) delete.execute());
        return result;
    }

    @Override
    public ArrayList<PendingDate> getPendingDates(int fileId) {
        GetPendingDates pendingDates = new GetPendingDates(fileId);
        ArrayList<PendingDate> pendingDateList = pendingDates.execute();
        return pendingDateList;
    }

    @Override
    public ArrayList<PendingFiles> getCurrentDaysPendingFiles(String startTime, String endTime) {

        GetCurrentDaysPendingFiles x = new GetCurrentDaysPendingFiles();
        x.setStartTime(startTime);
        x.setEndTime(endTime);

        ArrayList<PendingFiles> files = x.execute();
        return files;
    }

    @Override
    public ArrayList<SearchResult> getCheckedOutOrInFilesList(String fileCheckedInOut) {
        GetCheckOutOrInList getSearchFiles = new GetCheckOutOrInList(fileCheckedInOut);
        ArrayList<SearchResult> searchResults = getSearchFiles.execute();
        return searchResults;
    }

    @Override
    public ArrayList<Employee> getEmployeeList() {
        GetEmployeeList employeeList = new GetEmployeeList();
        ArrayList<Employee> list = employeeList.execute();
        return list;
    }

    @Override
    public Employee getEmployee(int employeeID) {
        GetEmployee getEmp = new GetEmployee(employeeID);
        Employee employee = getEmp.execute();
        return employee;
    }

    //Newly added functions
    @Override
    public User loginUser(String username, String password) {
        LoginUser loginUser = new LoginUser(username, password);
        User user = loginUser.execute();
        return user;
    }

    @Override
    public String retrievePID(String fName, String lName, Date dob) {
        RetrievePID retrievePID = new RetrievePID(fName, lName, dob);
        String pID = retrievePID.execute();
        return pID;
    }

    @Override
    public String insertNewPhoneConversation(String patientID, String whoCalled, String topic, String whatWasSaid) {

        InsertNewPhoneConversation insertNewPhoneConversation = new InsertNewPhoneConversation(patientID, whoCalled, topic, whatWasSaid);
        String pID = insertNewPhoneConversation.execute();
        return pID;
    }

    @Override
    public boolean createUser(User user) {

        CreateUser createUser = new CreateUser(user);
        Object created = createUser.execute();
        if (created.equals(true)) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public ArrayList<Patient> getPatients() {
        GetPatients getPatients = new GetPatients();
        ArrayList<Patient> patients = getPatients.execute();
        return patients;
    }

    @Override
    public ArrayList<PhoneConvRecord> getPatientPhoneConvs(String i) {
        GetPatientPhoneConvs getPatientPhoneConvs = new GetPatientPhoneConvs(i);
        ArrayList<PhoneConvRecord> patientPhoneRecords = getPatientPhoneConvs.execute();
        return patientPhoneRecords;
    }

    @Override
    public PhoneConvRecord getPhoneConversation(int i) {
        GetPhoneConversation getPhoneConversation = new GetPhoneConversation(i);
        PhoneConvRecord pcr = getPhoneConversation.execute();
        return pcr;
    }

    @Override
    public String updatePhoneConversation(String patientID, String whoCalled, String topic, String whatWasSaid) {
        UpdatePhoneConversation updatePhoneConversation = new UpdatePhoneConversation(patientID, whoCalled, topic, whatWasSaid);
        String pID = updatePhoneConversation.execute();
        return pID;
    }

    @Override
    public String archivePhoneRecord(PhoneConvRecord p) {
        ArchivePhoneRecord archivePhoneRecord = new ArchivePhoneRecord(p);
        String pcID = archivePhoneRecord.execute();
        return pcID;
    }

    @Override
    public String deletePhoneRecord(int i) {
        DeletePhoneRecord deletePhoneRecord = new DeletePhoneRecord(i);
        String pcID = deletePhoneRecord.execute();
        return pcID;
    }

    @Override
    public Boolean insertNewUser(String name, String em, String password, String access) {
        InsertNewUser insertNewUser = new InsertNewUser(name, em, password, access);
        Boolean result = insertNewUser.execute();
        return result;
    }

    @Override
    public ArrayList<User> getUsers() {
        GetUsers getUsers = new GetUsers();
        ArrayList<User> result = getUsers.execute();
        return result;
    }

    @Override
    public String deleteUser(String uid) {
        DeleteUser deleteUser = new DeleteUser(uid);
        String result = deleteUser.execute();
        return result;
    }

    @Override
    public boolean checkDupUserName(String userName) {

        CheckDupUser checkDupUser = new CheckDupUser(userName);
        Object isDuplicate = checkDupUser.execute();

        if (isDuplicate.equals(true)) {
            return true;
        } else {
            return false;
        }
    }
    
    @Override
    public boolean checkDupEmailId(String emailId) {

        CheckDupEmail checkDupEmail = new CheckDupEmail(emailId);
        Object isDuplicate = checkDupEmail.execute();

        if (isDuplicate.equals(true)) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public int insertRole(String roleName) {
        InsertRole insert = new InsertRole(roleName);
        int result = Integer.parseInt((String) insert.execute());
        return result;
    }

    @Override
    public Role getRole(String roleName) {
        GetRole getRole = new GetRole(roleName);
        Role role = getRole.execute();
        return role;
    }

    @Override
    public int insertResource(String resourceName, String resourceURL) {
        InsertResource insert = new InsertResource(resourceName, resourceURL);
        int result = Integer.parseInt((String) insert.execute());
        return result;
    }

    @Override
    public ArrayList<Resources> getResourceList() {
        GetResourceList resourceList = new GetResourceList();
        ArrayList<Resources> list = resourceList.execute();
        return list;
    }

    @Override
    public int insertRights(int roleId, int resourceId, int readEdit) {
        InsertRights right = new InsertRights(roleId, resourceId, readEdit);
        int result = Integer.parseInt((String) right.execute());
        return result;
    }

    public int changePassword(String CurrentPassword, String NewPassword, String username) {
        ChangePassword obj1 = new ChangePassword(CurrentPassword, NewPassword, username);
        int result = Integer.parseInt(obj1.execute());
        return result;
    }

    public int changeDefaultPassword(String NewPassword, String username) {
        ChangeDefaultPassword obj1 = new ChangeDefaultPassword(NewPassword, username);
        int result = Integer.parseInt(obj1.execute());
        return result;
    }

    public int forgotPassword(String username, String email, String password) {
        forgotPassword obj1 = new forgotPassword(username, email, password);
        int result = Integer.parseInt(obj1.execute());
        return result;
    }

    @Override
    public ArrayList<Role> getRoleList() {
        GetRoleList roleList = new GetRoleList();
        ArrayList<Role> list = roleList.execute();
        return list;
    }

    @Override
    public int updateRights(int roleId, int resourceId, int readEdit) {
        UpdateRights rights = new UpdateRights(roleId, resourceId, readEdit);
        int result = Integer.parseInt((String) rights.execute());
        return result;
    }

    @Override
    public ArrayList<RoleResourceRights> getRightsList(int roleId) {
        GetRightsList rightList = new GetRightsList(roleId);
        ArrayList<RoleResourceRights> list = rightList.execute();
        return list;
    }

    @Override
    public ArrayList<PendingFiles> getReportPendingFiles(String startTime, String endTime) {
        GetReportPendingFiles pendingFiles = new GetReportPendingFiles(startTime, endTime);
        ArrayList<PendingFiles> files = pendingFiles.execute();
        return files;
    }

    @Override
    public ArrayList<Tests> getReportTests(String startTime, String endTime) {
        GetReportTest testFiles = new GetReportTest(startTime, endTime);
        ArrayList<Tests> files = testFiles.execute();
        return files;
    }

    @Override
    public ArrayList<Consults> getReportConsults(String startTime, String endTime) {
        GetReportConsult consultFiles = new GetReportConsult(startTime, endTime);
        ArrayList<Consults> files = consultFiles.execute();
        return files;
    }
}
